package com.netintech.project.common.fastdfs;

import com.netintech.core.web.domain.RequestResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * FastDFS附件操作接口
 *
 * @author admin
 */
@RestController
public class FileController {
    @Resource
    private FileDfsUtil fileDfsUtil ;

    private static final String UPLOAD_FAILUE = "上传失败";
    private static final String SERVICE_EXCEPTION = "服务异常";
    private static final String DELETE_SUCCESS = "删除成功";
    private static final String OK = "OK";

    @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
    public RequestResult uploadFile (@RequestParam("file") MultipartFile file){
        String result;
        try{
            String path = fileDfsUtil.upload(file) ;
            if (!StringUtils.isEmpty(path)){
                result = path ;
            } else {
                result = UPLOAD_FAILUE ;
            }
        } catch (Exception e){
            e.printStackTrace() ;
            result = SERVICE_EXCEPTION ;
        }
        return RequestResult.success(result);
    }
    /**
     * 文件删除
     */
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    public RequestResult deleteByPath (String filePathName){
        String result = fileDfsUtil.deleteFile(filePathName);
        if(OK.equals(result)){
            return RequestResult.success(DELETE_SUCCESS);
        }else{
            return RequestResult.error(result);
        }
    }




}
